<!-- 车辆列表 -->
<template>
  <div class="dashboard-container vehicle customer-list-box">
    <div class="app-container">
      <el-card
        class="search-card-box"
        shadow="never"
      >
        <el-form
          ref="vehicleForm"
          label-width="80px"
          :model="vehicleForm"
        >
          <div class="filter-container">
            <el-row :gutter="60">
              <el-col :span="8">
                <el-form-item
                  class="input_4"
                  label="车辆类型:"
                  style="margin-bottom: 20px"
                  label-width="75px"
                >
                  <el-select
                    v-model="vehicleForm.truckTypeId"
                    clearable
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in listSel"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  v-model="vehicleForm.licensePlate"
                  label="车牌号码:"
                  style="margin-bottom: 0px"
                  label-width="75px"
                >
                  <el-input
                    v-model="vehicleForm.licensePlate"
                    placeholder="请输入车牌号码"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-button
                  type="warning"
                  @click="search"
                >搜索</el-button>
                <el-button
                  class="reset-btn"
                  plain
                  @click="res"
                >{{ mingzi }}</el-button>
              </el-col>
            </el-row>
          </div>
        </el-form>
      </el-card>
      <TabChange
        :default-active-index="0"
        :setting-list="settingList"
        @tabChange="tabChange"
      ></TabChange>
      <div
        v-loading="listLoading"
        :class="{ 'loading-box': listLoading }"
        element-loading-text="加载中"
        style="margin-top: 20px"
      >
        <el-card
          class="table-card-box"
          shadow="never"
        >
          <el-button
            style="float: left; margin-bottom:20px;width:100px"
            type="warning"
            @click="showDialog = true"
          >新增车型</el-button>
          <el-table
            ref="multipleTable"
            :cell-style="rowStyle"
            :data="dataList"
            fit
            style="width: 100%"
            stripe
            :header-cell-style="{ background: 'rgba(250,252,255,1)' }"
          >
            <!-- 没有的时候显示 -->
            <div
              v-show="(!dataList || dataList.length <= 0)"
              slot="empty"
            >
              <img
                src="@/icons/pic-kong.png"
                alt="img"
                style="margin-top: 20px; width: 25%; height: 25%"
              />
              <p style="margin-top: -20px; padding-bottom: 0px">这里空空如也</p>
            </div>
            <el-card
              v-show="(!dataList || dataList.length <= 0)"
              slot="empty"
              class="table-empty-box"
              shadow="never"
            >
              <empty></empty>
            </el-card>
            <el-table-column
              align="center"
              type="index"
              width="100"
              label="序号"
            >
              <template slot-scope="scope">
                <span>{{
                  scope.$index + (waybillSearchFormData.page - 1) * waybillSearchFormData.pageSize + 1
                }}</span>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="车牌号码"
              width="160"
              prop="licensePlate"
            >
            </el-table-column>
            <el-table-column
              align="center"
              label="车辆类型"
              width="250"
              prop="truckTypeName"
            >
            </el-table-column>
            <el-table-column
              align="center"
              label="司机数量"
              prop="driverNum"
              min-width="130"
            >
            </el-table-column>
            <el-table-column
              align="center"
              label="车辆状态"
              width="160"
            >
              <template v-slot="{row}">
                <div
                  class="tableColumn-status"
                  :class="{ 'stop-use': !row.workStatus }"
                >
                  {{ row.workStatus ? '启用' : '停用' }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="GPS设备ID"
              width="160"
              prop="deviceGpsId"
            >
            </el-table-column>
            <el-table-column
              align="center"
              label="实载重量(T)"
              min-width="130"
              prop="allowableLoad"
            >
            </el-table-column>
            <el-table-column
              align="center"
              label="实载体积(M3)"
              min-width="130"
              prop="allowableVolume"
            >
            </el-table-column>
            <el-table-column
              fixed="right"
              label="操作"
              width="250"
              align="center"
            >
              <template v-slot="{ row }">
                <el-button
                  type="text"
                  size="49"
                  @click="tiao(row)"
                >查看详情</el-button>
                <span class="xian"> | </span>
                <el-button
                  :class="{red0: row.workStatus !== 0}"
                  type="text"
                  size="49"
                  @click="qiyong(row)"
                >{{ row.workStatus ? '停用':'启用' }}</el-button>
                <span class="xian"> | </span>
                <el-button
                  type="text"
                  size="49"
                  @click="driver(row)"
                >配置司机</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <div
            v-show="dataList && dataList.length > 0"
            class="pagination"
          >
            <div class="pages">
              <el-pagination
                :current-page="Number(waybillSearchFormData.page)"
                :total="Number(total)"
                :page-size="Number(waybillSearchFormData.pageSize)"
                :page-sizes="[10, 20, 30, 50]"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <VehicleAdd
      :show-dialog.sync="showDialog"
      :list-sel="listSel"
    />
    <ArrangeDriverVue
      ref="ArrangeDriverVue"
      :dialog-form-visible.sync="dialogFormVisible"
    />
  </div>
</template>
<script>
import VehicleAdd from './components/vehicle-add.vue'
import TabChange from '@/components/TabChange'
import { _getVehicleList, _getVehicleSel, _qiyong, _tingyong } from '@/api/vehicle'
import { unBindDriverList } from '@/api/transit'
import ArrangeDriverVue from './components/arrange-driver.vue'
export default {
  name: 'Vehicle',
  components: {
    TabChange,
    VehicleAdd,
    ArrangeDriverVue
  },
  data() {
    return {
      mingzi: '重置',
      flag2: 0,
      flag3: 0,
      listLoading: true,
      dialogFormVisible: false,
      showDialog: false,
      activeIndex: 0,
      total: null,
      waybillSearchFormData: {
        licensePlate: '',
        page: 1,
        pageSize: 10,
        truckTypeId: ''
        // workStatus: ''
      },
      dataList: [],
      listSel: [],
      vehicleForm: {
        licensePlate: '',
        truckTypeId: ''
      },
      settingList: [
        {
          value: 0,
          label: '全部',
          num: 0
        },
        {
          value: 1,
          label: '可用',
          num: 0
        },
        {
          value: 2,
          label: '停用',
          num: 0
        }
      ]
    }
  },
  created() {
    this.getVehicleList()
    this.getVehicleSel()
    // 键盘enter操作
    const _this = this
    document.onkeydown = function (e) {
      var key = window.event.keyCode
      if (key === 13) {
        _this.search()
      }
    }
  },
  methods: {
    tiao(row) {
      this.$router.push(`/transit/vehicle-detail/${row.id}`)
    },
    res() {
      this.mingzi = '重置'
      this.vehicleForm.licensePlate = ''
      this.vehicleForm.truckTypeId = ''
      this.waybillSearchFormData.licensePlate = ''
      this.waybillSearchFormData.truckTypeId = ''
      this.getVehicleSel()
    },
    // 栓选过滤
    search() {
      // this.flag2 = 1
      this.flag3 = 1
      this.mingzi = '请重置'
      this.waybillSearchFormData.licensePlate = this.vehicleForm.licensePlate.trim()
      this.waybillSearchFormData.truckTypeId = this.vehicleForm.truckTypeId
      if (this.waybillSearchFormData.licensePlate === '' && this.waybillSearchFormData.truckTypeId === '') {
        this.mingzi = '重置'
      }
      this.getVehicleSel()
    },
    driver(row) {
      this.$confirm(`配置司机需满足以下条件：<br>1、车辆信息已完善<br>2、车辆无未完成运输任务`, '车辆配置', {
        dangerouslyUseHTMLString: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.$refs.ArrangeDriverVue.carModalInfo = {
          licensePlate: row.licensePlate,
          truckTypeName: row.truckTypeName,
          status: row.status,
          allowableLoad: row.allowableLoad,
          allowableVolume: row.allowableVolume,
          truckId: row.id
        }
      }).then(async() => {
        // 这是司机数组
        try {
          this.dialogFormVisible = true
          const res = await unBindDriverList()
          this.$refs.ArrangeDriverVue.driversList = await res.data
          this.$refs.ArrangeDriverVue.getDriverListByCar()
        } catch (err) {
          // 出错
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    qiyong(row) {
      if (row.workStatus === 0) {
        this.$confirm(`确定要启用吗?<br>车辆启用需满足以下条件：<br>1、车辆信息已完善<br>2、车辆绑定司机>=2，且已设置排班`, '车辆启用', {
          dangerouslyUseHTMLString: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          try {
            const res = await _qiyong(row.id)
            if (res.code === 0) {
              this.$message.success('成功')
            }
            if (res.code === 1) {
              this.$message.warning(res.msg)
            }
            this.getCarModelsList()
          } catch (err) {
          // this.$message.warning('失败')
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      } else { // 停用
        const h = this.$createElement
        this.$msgbox({
          title: '车辆停用',
          message: h('div', null, [h('p', null, '确定要停用吗？'), h('p', null, '车辆停用需满足以下条件：'), h('p', null, '1车辆当前无运输任务'), h('p', null, '2车辆无关联运输线路'), h('p', { style: 'color: rgb(255, 179, 2' }, '注：停用后司机将自动解除绑定')]),
          showCancelButton: true,
          dangerouslyUseHTMLString: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          try {
            const res = await _tingyong(row.id)
            if (res.code === 200) {
              this.$message.success('成功')
            }
            if (res.code === 1) {
              this.$message.warning(res.msg)
            }
            this.getCarModelsList()
          } catch (err) {
          // this.$message.warning('失败')
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      }
    },
    handleSizeChange(val) {
      this.waybillSearchFormData.pageSize = val
      this.waybillSearchFormData.page = 1
      this.getVehicleSel()
    },
    handleCurrentChange(val) {
      this.waybillSearchFormData.page = val
      this.getVehicleSel()
    },
    rowStyle() {
      return 'text-align:center'
    },
    // 切换框
    async tabChange(activeIndex) {
      if (activeIndex === 0) {
        this.activeIndex = 0
        this.waybillSearchFormData.page = 1
      } else if (activeIndex === 1) {
        this.activeIndex = 1
        this.waybillSearchFormData.page = 1
      } else if (activeIndex === 2) {
        this.activeIndex = 2
        this.waybillSearchFormData.page = 1
      }
      this.getVehicleSel(this.waybillSearchFormData)
    },
    async getVehicleList() {
      const res = await _getVehicleList()
      this.listSel = res.data
    },
    // 全部获取
    async getVehicleSel(obj) {
      this.listLoading = true
      // 如果没有搜索显示这个
      // if (this.flag2 === 0) {
      // 全部获取
      if (this.flag3 === 1) {
        this.waybillSearchFormData.page = 1
      }
      this.waybillSearchFormData.workStatus = ''
      const res = await _getVehicleSel(this.waybillSearchFormData)
      this.dataList = res.data.items
      this.total = res.data.counts

      // 获取可用
      this.waybillSearchFormData.workStatus = 1
      const res1 = await _getVehicleSel(this.waybillSearchFormData)

      // 获取停用
      this.waybillSearchFormData.workStatus = 0
      const res2 = await _getVehicleSel(this.waybillSearchFormData)

      this.waybillSearchFormData.workStatus = ''
      this.settingList[0].num = res.data.counts
      this.settingList[1].num = res1.data.counts
      this.settingList[2].num = res2.data.counts
      // 选择渲染
      if (this.activeIndex === 0) {
        this.dataList = res.data.items
        this.total = res.data.counts
      }
      if (this.activeIndex === 1) {
        this.dataList = res1.data.items
        this.total = res1.data.counts
      }
      if (this.activeIndex === 2) {
        this.dataList = res2.data.items
        this.total = res2.data.counts
      }
      this.flag3 = 0
      this.listLoading = false
    }
  }

}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.red0 {
  color: #f56c6c;
}
.stop {
  color: #f56c6c;
}
.tableColumn-status {
  justify-content: center;
}
.input_4 {
  margin-bottom: 0px !important;
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
}
.vehicle /deep/ .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

.vehicle {
  /deep/ .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  /deep/ .el-dialog__body {
    text-align: center;
    padding: 20px 20px 0px 20px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }
}

// 下拉框
/deep/ .el-select {
  width: 100%;
}
</style>

<style>
.reset-btn {
  width: 90px;
  height: 40px;
}
  .startconfirm{
    width: 481px;

    }
    .startconfirm .el-message-box__header{
      padding-bottom: 4px;
    }
    .startconfirm .el-message-box__container{
      margin-bottom: 15px;
    }
    .confirm .el-message-box__status.el-icon-warning{
      left: 70px;
    }
    .confirm.el-message-box__title{
      font-size: 16px;
    }
    .confirm .el-message-box__message p {
      color: #F92D2D;
      margin-top: 5px;
    }
    .confirm .el-message-box__btns{
      text-align: center;
      margin-top: 10px;
    }
</style>
